package org.loofer.ext.component.info;

import org.loofer.ext.utils.StringUtils;
import java.util.HashMap;
import java.util.Map;

public enum ServiceProvider {
    NONE(StringUtils.NOT_AVAILABLE),
    NEVER_HEARD("Unknown"),
    CHINA_MOBILE("China Mobile"),
    CHINA_UNICOM("China Unicom"),
    CHINA_TELECOM("China Telecom");
    
    private static final Map<String, ServiceProvider> IMSI_PROVIDER_MAP = null;
    private String name;

    static {
        IMSI_PROVIDER_MAP = new HashMap();
        IMSI_PROVIDER_MAP.put("46000", CHINA_MOBILE);
        IMSI_PROVIDER_MAP.put("46002", CHINA_MOBILE);
        IMSI_PROVIDER_MAP.put("46007", CHINA_MOBILE);
        IMSI_PROVIDER_MAP.put("46003", CHINA_TELECOM);
        IMSI_PROVIDER_MAP.put("46005", CHINA_TELECOM);
        IMSI_PROVIDER_MAP.put("46001", CHINA_UNICOM);
        IMSI_PROVIDER_MAP.put("46006", CHINA_UNICOM);
        IMSI_PROVIDER_MAP.put("46020", CHINA_MOBILE);
    }

    private ServiceProvider(String name) {
        setName(name);
    }

    public static ServiceProvider fromIMSI(String IMSI) {
        if (IMSI == null) {
            return NONE;
        }
        if (IMSI.length() < 5) {
            return NONE;
        }
        ServiceProvider provider = (ServiceProvider) IMSI_PROVIDER_MAP.get(IMSI.substring(0, 5));
        return provider == null ? NEVER_HEARD : provider;
    }

    public String getName() {
        return this.name;
    }

    private void setName(String name) {
        this.name = name;
    }

    public String toString() {
        return getName();
    }
}
